package com.iknow.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Binder;
import android.os.Parcel;
import android.os.RemoteException;
import com.iknow.util.Loger;
import com.iknow.util.StringUtil;
import java.io.File;
import java.io.FileInputStream;

public class MusicPlayBinder extends Binder
{
  public static int CODE_GET_CURRENT_POISION;
  public static int CODE_IS_PLAYING;
  public static int CODE_PAUSE;
  public static int CODE_PLAY;
  public static int CODE_RELEASE;
  public static int CODE_SEEK_TO = 0;
  public static int CODE_SET_PLAY_TIME;
  public static int CODE_START;
  public static int CODE_STOP_PLAY;
  private String mMediaSrc;
  private Service mService;
  protected MediaPlayer mediaPlayer = new MediaPlayer();

  static
  {
    CODE_GET_CURRENT_POISION = 1;
    CODE_IS_PLAYING = 2;
    CODE_SET_PLAY_TIME = 3;
    CODE_STOP_PLAY = 4;
    CODE_PLAY = 5;
    CODE_PAUSE = 6;
    CODE_START = 7;
    CODE_RELEASE = 8;
  }

  public MusicPlayBinder(Service paramService)
  {
    this.mService = paramService;
  }

  private void onPlayError()
  {
    Intent localIntent = new Intent("com.iknow.playservice");
    localIntent.putExtra("code", -1);
    this.mService.sendBroadcast(localIntent);
  }

  private void play(int paramInt)
  {
    if (StringUtil.isEmpty(this.mMediaSrc))
      return;
    try
    {
      FileInputStream localFileInputStream = new FileInputStream(this.mMediaSrc);
      this.mediaPlayer.reset();
      this.mediaPlayer.setDataSource(localFileInputStream.getFD());
      this.mediaPlayer.prepare();
      this.mediaPlayer.seekTo(paramInt);
      this.mediaPlayer.start();
      Intent localIntent = new Intent("com.iknow.playservice");
      localIntent.putExtra("duration", this.mediaPlayer.getDuration());
      this.mService.sendBroadcast(localIntent);
      this.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(this)
      {
        public void onCompletion()
        {
          if (MusicPlayBinder.access$0(this.this$0) != null)
          {
            Intent localIntent = new Intent("com.iknow.playservice");
            localIntent.putExtra("code", 0);
            MusicPlayBinder.access$0(this.this$0).sendBroadcast(localIntent);
          }
        }
      });
      this.mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener(this)
      {
        public boolean onError(, int paramInt1, int paramInt2)
        {
          if (MusicPlayBinder.access$0(this.this$0) != null)
            MusicPlayBinder.access$1(this.this$0);
          return false;
        }
      });
    }
    catch (Exception localException)
    {
      new File(this.mMediaSrc).delete();
      onPlayError();
      localException.printStackTrace();
    }
  }

  protected boolean onTransact(int paramInt1, Parcel paramParcel1, Parcel paramParcel2, int paramInt2)
    throws RemoteException
  {
    switch (paramInt1)
    {
    case 3:
    default:
    case 0:
    case 1:
    case 2:
    case 4:
    case 5:
    case 6:
    case 7:
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  while (true)
                  {
                    return super.onTransact(paramInt1, paramParcel1, paramParcel2, paramInt2);
                    int i = (int)paramParcel1.readLong();
                    this.mediaPlayer.seekTo(i);
                  }
                  paramParcel2.writeInt(this.mediaPlayer.getCurrentPosition());
                }
                boolean[] arrayOfBoolean = new boolean[1];
                arrayOfBoolean[0] = this.mediaPlayer.isPlaying();
                paramParcel2.writeBooleanArray(arrayOfBoolean);
              }
              this.mediaPlayer.stop();
            }
            play((int)paramParcel1.readLong());
          }
          this.mediaPlayer.pause();
        }
        this.mediaPlayer.start();
      }
    case 8:
    }
    try
    {
      this.mediaPlayer.release();
    }
    catch (Exception localException)
    {
      Loger.e("MusicBinder", localException.getMessage());
    }
  }

  public void setMediaSource(String paramString)
  {
    this.mMediaSrc = paramString;
  }
}